import org.jbox2d.common.Vec2;
import pulpcore.math.CoreMath;

/**
 * Created by will
 * Date: May 17, 2008 2:50:47 PM
 */
public class ComputerPlayer extends Player {

    public ComputerPlayer(Arena physics, double x, double y, double r, float sharpness) {
        super(physics, x, y, r, sharpness);
    }

    public void update(int elapsedTime) {
        super.update(elapsedTime);
        Vec2 target = ((Arena)physics).getNearestTarget(new Vec2((float)this.x.get(),(float)this.y.get()));
        target.y *= -1;
        // Orient the ComputerPlayer towards the nearest target.
        double dy = target.y - getBody().getWorldCenter().y;
        double dx = target.x - getBody().getWorldCenter().x;
        int i = -CoreMath.atan2(CoreMath.toFixed(dy), CoreMath.toFixed(dx)) + CoreMath.ONE_HALF_PI;

        // This check is to ensure the shortest rotation direction between angles
        if(CoreMath.abs(angle.getAsFixed()-i) > CoreMath.PI) {
            if(i > angle.getAsFixed()) angle.setAsFixed(angle.getAsFixed() + CoreMath.TWO_PI);
            else angle.setAsFixed(angle.getAsFixed() - CoreMath.TWO_PI);
        }
        angle.animateToFixed(i,100);

        if(canFire()) {
            this.fireShot(target.x,target.y);
        }
    }
}
